package com.spsyry.product.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSON;
import com.spsyry.common.core.utils.DateUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import com.spsyry.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.spsyry.product.domain.ProductDetail;
import com.spsyry.product.mapper.ProductMapper;
import com.spsyry.product.domain.Product;
import com.spsyry.product.service.IProductService;

/**
 * 商品表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-28
 */
@Service
public class ProductServiceImpl implements IProductService 
{
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询商品表
     * 
     * @param id 商品表主键
     * @return 商品表
     */
    @Override
    public Product selectProductById(Long id)
    {
        return productMapper.selectProductById(id);
    }

    /**
     * 查询商品表列表
     * 
     * @param product 商品表
     * @return 商品表
     */
    @Override
    public List<Product> selectProductList(Product product)
    {
        return productMapper.selectProductList(product);
    }

    /**
     * 新增商品表
     * 
     * @param product 商品表
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProduct(Product product)
    {
        product.setCreateTime(DateUtils.getNowDate());
        int rows = productMapper.insertProduct(product);
        insertProductDetail(product);
        return rows;
    }

    /**
     * 修改商品表
     * 
     * @param product 商品表
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProduct(Product product)
    {
        product.setUpdateTime(DateUtils.getNowDate());
        productMapper.deleteProductDetailByProductId(product.getId());
        insertProductDetail(product);
        return productMapper.updateProduct(product);
    }

    /**
     * 批量删除商品表
     * 
     * @param ids 需要删除的商品表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductByIds(Long[] ids)
    {
        productMapper.deleteProductDetailByProductIds(ids);
        return productMapper.deleteProductByIds(ids);
    }

    /**
     * 删除商品表信息
     * 
     * @param id 商品表主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProductById(Long id)
    {
        productMapper.deleteProductDetailByProductId(id);
        return productMapper.deleteProductById(id);
    }

    /**
     * 新增商品详情信息
     * 
     * @param product 商品表对象
     */
    public void insertProductDetail(Product product)
    {
        List<ProductDetail> productDetailList = product.getProductDetailList();
        Long id = product.getId();
        if (StringUtils.isNotNull(productDetailList))
        {
            List<ProductDetail> list = new ArrayList<ProductDetail>();
            for (ProductDetail productDetail : productDetailList)
            {
                productDetail.setProductId(id);
                list.add(productDetail);
            }
            if (list.size() > 0)
            {
                productMapper.batchProductDetail(list);
            }
        }
    }
}
